import os
from tqdm import tqdm
import shutil

imagePath="G:\数据集\植保所\分类目标图片\斑潜蝇危害状\原始数据"
savePath="G:\数据集\植保所\分类目标图片\斑潜蝇危害状\分类后数据"

def checkDirForDeepIsExists(dir_name):
    if not os.path.exists(dir_name):  # os模块判断并创建
        deep_dir_name = os.path.dirname(dir_name)
        checkDirForDeepIsExists(deep_dir_name)
        os.mkdir(dir_name)

for dirpath, dirnames, filenames in os.walk(imagePath):
    for filename in tqdm(filenames):
        imageAllPath=os.path.join(dirpath, filename)
        #print(os.path.join(dirpath, filename))
        if("葱" in imageAllPath):
            newImgPath=imageAllPath.replace(imagePath,savePath+"\葱")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            shutil.copy(imageAllPath,newImgPath)
        if ("番茄" in imageAllPath):
            newImgPath = imageAllPath.replace(imagePath, savePath + "\番茄")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            shutil.copy(imageAllPath, imageAllPath.replace(imagePath, savePath + "\番茄"))
        if ("三叶" in imageAllPath):
            newImgPath = imageAllPath.replace(imagePath, savePath + "\三叶")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            shutil.copy(imageAllPath, imageAllPath.replace(imagePath, savePath + "\三叶"))
        if ("美洲" in imageAllPath):
            newImgPath = imageAllPath.replace(imagePath, savePath + "\美洲")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            shutil.copy(imageAllPath, imageAllPath.replace(imagePath, savePath + "\美洲"))
        if ("南美" in imageAllPath):
            newImgPath = imageAllPath.replace(imagePath, savePath + "\南美")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            shutil.copy(imageAllPath, imageAllPath.replace(imagePath, savePath + "\南美"))
        if ("豌豆" in imageAllPath):
            newImgPath = imageAllPath.replace(imagePath, savePath + "\豌豆")
            dir_name = os.path.dirname(newImgPath)
            checkDirForDeepIsExists(dir_name)
            #print(os.path.join(dirpath, filename))
            shutil.copy(imageAllPath, imageAllPath.replace(imagePath, savePath + "\豌豆"))
